package com.kinghood.productcenter.service.base.handler;

import cn.hutool.core.date.DateUtil;
import com.kinghood.infrastructure.util.UserUtil;
import com.kinghood.infrastructure.util.base.IdGenUtil;
import com.kinghood.productcenter.common.model.dao.base.BaseAttributeDO;
import com.kinghood.productcenter.common.model.dao.base.BaseProductTemplateGitemDO;
import com.kinghood.productcenter.common.model.dao.base.BaseProductTemplateGroupDO;
import com.kinghood.productcenter.common.model.dao.base.BaseProductTemplateRuleDO;
import com.kinghood.productcenter.common.model.dto.base.GetNodeDTO;
import com.kinghood.productcenter.common.model.dto.product.ProductTemplateNodeAttributeDTO;
import com.kinghood.productcenter.common.model.dto.product.ProductTemplateNodeDTO;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2024-01-25 15:20
 **/
public class ProductTemplateHandler {

    /**
     * 获取节点数据
     */
    public static void getNode(GetNodeDTO in,
                               Long parentId,
                               List<ProductTemplateNodeDTO> nodeList,
                               String idPath) {

        List<BaseProductTemplateGroupDO> list = in.getList();
        Long productTemplateId = in.getProductTemplateId();
        if (CollectionUtils.isEmpty(nodeList)) {
            return;
        }
        int sort = 1;
        String[] split = idPath.split(",");
        int level = split.length;
        if(level > 20){
            return;
        }
        for (ProductTemplateNodeDTO node : nodeList) {
            BaseProductTemplateGroupDO one = new BaseProductTemplateGroupDO();
            one.setId(IdGenUtil.getId());
            one.setTemplateId(productTemplateId);
            one.setParentId(parentId);
            one.setGroupCode(node.getGroupCode());
            one.setGroupName(node.getGroupName());
            if(level==1){
                one.setIdPath(one.getId().toString());
            } else {
                one.setIdPath(idPath+","+one.getId());
            }
            one.setLevel(level);
            one.setSort(sort);
            one.setIsFixed(node.getIsFixed());
            one.setIsMultiple(node.getIsMultiple());
            one.setCreateTime(LocalDateTime.now());
            one.setUpdateTime(LocalDateTime.now());
            one.setCreateUserId(UserUtil.getUserId());
            one.setUpdateUserId(UserUtil.getUserId());
            if (StringUtils.isEmpty(one.getGroupCode())) {
                one.setGroupCode("TG" + DateUtil.format(new Date(), "yyyy-MM-dd_HH_mm_ss"));
            }
            list.add(one);

            setItem(one,in, node.getAttributeList(), sort * 1000,null);

            if (!CollectionUtils.isEmpty(node.getSunNodeList())) {
                getNode(in, one.getId(), node.getSunNodeList(), one.getIdPath());
            }
            sort++;
        }
    }

    /**
     * 封装属性
     * */
    public static void setItem(BaseProductTemplateGroupDO group,
                               GetNodeDTO in,
                               List<ProductTemplateNodeAttributeDTO> attributeList,
                               int sort,
                               BaseProductTemplateGitemDO parentItem){

        List<BaseProductTemplateGitemDO> itemList = in.getItemList();
        List<BaseProductTemplateRuleDO> ruleList = in.getRuleList();
        if(CollectionUtils.isEmpty(attributeList) || itemList.size() > 1000){
            return;
        }

        int s = sort;
        int sun = sort;
        for (ProductTemplateNodeAttributeDTO attributeDTO : attributeList) {
            BaseProductTemplateGitemDO item = new BaseProductTemplateGitemDO();
            item.setId(IdGenUtil.getId());
            item.setTemplateId(group.getTemplateId());
            item.setGroupId(group.getId());
            item.setAttributeId(attributeDTO.getAttributeId());
            BaseAttributeDO attributeDO = in.getAttributeIdMap().get(item.getAttributeId());
            if(Objects.nonNull(attributeDO)){
                item.setAttributeCode(attributeDO.getAttributeCode());
            }
            item.setDefaultValue(attributeDTO.getDefaultValue());
            item.setRequired(attributeDTO.getRequired());
            item.setAutofill(attributeDTO.getAutofill());
            item.setSort(s);
            item.setCreateTime(LocalDateTime.now());
            item.setUpdateTime(LocalDateTime.now());
            item.setCreateUserId(UserUtil.getUserId());
            item.setUpdateUserId(UserUtil.getUserId());
            itemList.add(item);
            s++;
            sun = sun + sort/10;

            if(Objects.nonNull(parentItem)){
                BaseProductTemplateRuleDO r = new BaseProductTemplateRuleDO();
                r.setId(IdGenUtil.getId());
                r.setTemplateId(group.getTemplateId());
                r.setGitemId(parentItem.getId());
                r.setAttributeId(parentItem.getAttributeId());
                r.setAttributeCode(parentItem.getAttributeCode());
                r.setBindGitemId(item.getId());
                r.setBindAttributeId(item.getAttributeId());
                r.setBindAttributeCode(item.getAttributeCode());
                r.setTriggerValue(attributeDTO.getTriggerValue());
                r.setCreateTime(LocalDateTime.now());
                r.setUpdateTime(LocalDateTime.now());
                r.setCreateUserId(UserUtil.getUserId());
                r.setUpdateUserId(UserUtil.getUserId());
                ruleList.add(r);
            }

            setItem(group,in,attributeDTO.getTriggerList(),sun,item);
        }
    }

}
